package com.graduation.springbootinit.controller;

import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.common.ResultUtils;
import com.graduation.springbootinit.exception.BusinessException;
import com.graduation.springbootinit.model.entity.ShopPart.PlatformMonthlyIncome;
import com.graduation.springbootinit.service.PlatformMonthlyIncomeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/platform")
public class PlatformController {

    @Resource
    private PlatformMonthlyIncomeService platformMonthlyIncomeService;

    /**
     * 统计并更新指定年月的平台收入数据
     */
    @PostMapping("/monthly-income/calculate")
    public BaseResponse<Void> calculateMonthlyIncome(@RequestParam Integer year, @RequestParam Integer month) {
        if (year == null || month == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (month < 1 || month > 12) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "月份必须在1-12之间");
        }
        platformMonthlyIncomeService.calculateAndUpdateMonthlyIncome(year, month);
        return ResultUtils.success(null);
    }

    /**
     * 获取指定年月的平台收入数据
     */
    @GetMapping("/monthly-income")
    public BaseResponse<PlatformMonthlyIncome> getMonthlyIncome(@RequestParam Integer year, @RequestParam Integer month) {
        if (year == null || month == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (month < 1 || month > 12) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "月份必须在1-12之间");
        }
        return platformMonthlyIncomeService.getMonthlyIncome(year, month);
    }

    /**
     * 获取指定年份的所有月度收入数据
     */
    @GetMapping("/yearly-income")
    public BaseResponse<List<PlatformMonthlyIncome>> getYearlyIncome(@RequestParam Integer year) {
        if (year == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return platformMonthlyIncomeService.getYearlyIncome(year);
    }

    /**
     * 获取指定年月的总佣金收入
     */
    @GetMapping("/monthly-commission")
    public BaseResponse<BigDecimal> getTotalCommission(@RequestParam Integer year, @RequestParam Integer month) {
        if (year == null || month == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (month < 1 || month > 12) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "月份必须在1-12之间");
        }
        return platformMonthlyIncomeService.getTotalCommission(year, month);
    }

    /**
     * 获取指定年月的活跃商铺数
     */
    @GetMapping("/active-stores")
    public BaseResponse<Integer> getActiveStoresCount(@RequestParam Integer year, @RequestParam Integer month) {
        if (year == null || month == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (month < 1 || month > 12) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "月份必须在1-12之间");
        }
        return platformMonthlyIncomeService.getActiveStoresCount(year, month);
    }
}